/**
 * grids
 */

var lookUserConsumptionDetailGrid = function(userId) {
	// 定义表的每列（对应表头信息）
	var userConsumptionDManagerCm = new Ext.grid.ColumnModel([new Ext.grid.RowNumberer(),
			{
				dataIndex : "userId",
				header : "用户编号",
				sortable : true,
				width : 70
			}, {
				dataIndex : "userName",
				header : "用户名",
				sortable : true,
				width : 70
			}, {
				dataIndex : "orderCode",
				header : "订单编码",
				sortable : true,
				width : 160,
				renderer : function(value, meta, record) {
					if (value) {
						meta.attr = 'ext:qtitle="订单编码:" ext:qtip="' + value
								+ '"';
						return value;
					}
				}
			}, {
				dataIndex : "orderType",
				header : "订单类型",
				sortable : false,
				width : 70,
				renderer : function(value) {
					return PmsUtil.OrderType(value);
				}
			}, {
				dataIndex : "orderStatus",
				header : "订单状态",
				sortable : false,
				width : 70,
				renderer : function(value) {
					if (value) {
						return '<font color=\"#FF0000\" >'
								+ PmsUtil.OrderStatus(value) + '</font>';
					}
				}
			}, {
				header : "商品名称",
				width : 180,
				sortable : true,
				dataIndex : 'goodsName',
				renderer : function(value, meta, record) {
					if (value) {
						meta.attr = 'ext:qtitle="商品名称:" ext:qtip="' + value
								+ '"';
						return value;
					}
				}
			}, {						
				dataIndex : "totalAmount",
				header : "订单总金额",
				sortable : false,
				width : 80,
				renderer : function(value) {
					return Ext.util.Format.number(value, '0,0.00');
				}
			}, {
				dataIndex : "payAmount",
				header : "实际支付金额",
				sortable : false,
				width : 90,
				renderer : function(value) {
					return PmsUtil.Price(value);
				}
			}, {
				dataIndex : "week",
				id:"uc_week",
				header : "距上次购买天数",
				sortable : false,
				width : 110
			}, {
				dataIndex : "createdDate",
				header : "创建时间",
				sortable : false,
				width : 130
			}]);

	// 定义分析器
	var userConsumptionManagerReader = new Ext.data.JsonReader({
				totalProperty : "totalCount",
				root : "list",
				fields : [{
							name : "userId"
						}, {
							name : "userName"
						}, {
							name : "orderCode"
						}, {
							name : "orderType"
						},{
							name : "orderStatus"
						}, {
							name : "goodsName"
						}, {
							name : "totalAmount"
						}, {
							name : "payAmount"
						}, {
							name : "week"
						}, {
							name : "createdDate"
						}]
			});

	// 取数据store
	var userConsumptionViewStore = new Ext.data.Store({
				autoLoad : true,// 要加载数据必须设置为true
				proxy : new Ext.data.HttpProxy({
							url : __ctxPath + "/statisticManager/selectSatasticUserConsumptionDetail"
						}),
				remoteSort : false,
				baseParams : {
					userId:userId,
					start : 0,
					limit : 25
				},
				listeners : {
					beforeload : {
						fn : function(thiz, options) {
						}
					}
				},
				reader : userConsumptionManagerReader
			});
			
		userConsumptionViewStore.addListener('load', function(st, rds, opts) {
			        for( var c=0; c<rds.length; c++ ) {
			        	if(c==0){
			        		st.getAt(c).set('week', 0); 
			        		var record = st.getAt(c);
			        		record.dirty=false; 
			        		record.commit(); 
			        	}else{
			        		var e = Ext.util.Format.date(rds[c].data.createdDate, 'Y-m-d');
			        		var f = Ext.util.Format.date(rds[c-1].data.createdDate, 'Y-m-d');
			        		var g = new Date(e); 
               				var h = new Date(f);
			        		var a = rds[c-1].data.createdDate;
			        		var elapsed = Math.round((g.getTime() - h.getTime())/(86400000));
			        		st.getAt(c).set('week', elapsed);
			        		var record = st.getAt(c);
			        		record.dirty=false; 
			        		record.commit(); 
			        	}
			        }
			});

	var userConsumptionDetailGrid = new Ext.grid.EditorGridPanel({
				id : 'userConsumptionDetailGrid',
				store : userConsumptionViewStore,
				cm:userConsumptionDManagerCm,
				autoScroll : true,
				clicksToEdit : 1,
				loadMask : true,
				trackMouseOver : true,
				stripeRows : true,
				columnLines : true,
				loadMask : {
					msg : "正在加载数据，请稍侯……"
				},
				viewConfig : {
					emptyText : '没有您想要的数据!'
				},
				anchor : '100% 100%',
				frame : true,
				bbar : new PG.PagingBar({
							store : userConsumptionViewStore
						})
			});
	return userConsumptionDetailGrid;
};


/**
 * 订单详情Form查询
 * 
 * @param {}
 *            orderId
 */
var userConsumptionDetailFormLoad = function(userId) {
	if (userId != null && userId != 'undefined') {
		Ext.getCmp("lookUserConsumptionDetailForm").getForm().load({
			deferredRender : false,
			timeout : 1800000,
			url : __ctxPath + "/statisticManager/selectSatasticUserConsumptionDetail",
			params : {
				userId : userId
			},
			root : "data",
			waitTitle : "请稍候",
			waitMsg : "正在载入数据，请稍候......",
			success : function(response, options) {
				var data = options.result.data;
				console.log(data);
				Ext.getCmp("userConsumptionManagerGrid").getStore().reload();
			},
			failure : function(response, options) {
				Ext.MessageBox.show({
							title : "错误提示",
							msg : "载入失败,请稍后再试!",
							buttons : Ext.MessageBox.OK,
							icon : Ext.MessageBox.ERROR
						});
			}
		});
	};
};